package agu.misc;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;

public class ActionBarUtil extends Fragment
{
    public static ActionBarUtil with(LifecycleOwner owner){
        return ActivityUtil.with(owner, ActionBarUtil.class);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item){
        //响应点击事件
        if(item.getItemId() == android.R.id.home){
            Activity activity = getActivity();
            if(activity instanceof AppCompatActivity){
                if(((AppCompatActivity)activity).onSupportNavigateUp()){//原配优先
                    return true;
                }
                activity.onBackPressed();
            }
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    static ActivityLifecycleCallback2 ALC;

    public static void fixActionBar(Application app, boolean center){
        ALC = ALC != null? ALC: new ActivityLifecycleCallback2(null)
        {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle){
                fixActionBar(activity, center);
            }
        };
        app.unregisterActivityLifecycleCallbacks(ALC);
        app.registerActivityLifecycleCallbacks(ALC);
    }

    public static void fixActionBar(Activity activity, boolean center){
        if(activity instanceof AppCompatActivity){
            ActionBar actionBar = ((AppCompatActivity)activity).getSupportActionBar();
            if(actionBar == null) return;

            if(!activity.isTaskRoot()){
                actionBar.setDisplayHomeAsUpEnabled(true);//显示返回按钮
                ActionBarUtil.with((AppCompatActivity)activity).setHasOptionsMenu(true);//接收点击事件
            }

            if(center && actionBar.getCustomView() == null){
                CharSequence title = actionBar.getTitle();

                //更换title
                TextView customView = new TextView(activity);
                customView.setText(title);

                actionBar.setDisplayShowCustomEnabled(true);
                actionBar.setCustomView(customView);//设置customView，以便找到actionBarView

                ViewGroup actionBarView = (ViewGroup)customView.getParent();//找到actionBarView
                actionBar.setDisplayShowCustomEnabled(false);//取消customView

                //原配titleView
                TextView titleView = null;
                for(int i = 0; i < actionBarView.getChildCount(); i++){
                    View child = actionBarView.getChildAt(i);
                    if(child instanceof TextView && ((TextView)child).getText().equals(title)){
                        titleView = (TextView)child;
                        break;
                    }
                }
                assert titleView != null;

                //居中显示title
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(-2, -2, Gravity.CENTER);
                lp.leftMargin = lp.rightMargin = Math.round(titleView.getTextSize() * 4);
                //actionBarView.removeView(customView);
                ((ViewGroup)actionBarView.getParent()).addView(customView, lp);

                //复制属性
                customView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleView.getTextSize());
                customView.setTextColor(titleView.getTextColors());
                customView.setTypeface(titleView.getTypeface());

                //更新title
                titleView.addTextChangedListener(new TextWatcher2(customView::setText));
                titleView.setVisibility(View.INVISIBLE);//隐藏原配titleView
                //actionBar.setDisplayShowTitleEnabled(false);
            }
        }
    }

}
